-module(wea_proc).
-export([process/0, process/1]).

process() ->
    process("weather.dat").

process(FileName) -> 

    SortPred = fun({_Day1, MxT1, MnT1}, {_Day2, MxT2, MnT2}) -> 
            MxT1 - MnT1 > MxT2 - MnT2
    end,
    IsDataLine = fun(Line) -> 
            file_common:is_int_string(string:sub_word(Line, 1))
    end,
    LineParser = fun(Line) ->
            DayStr = string:sub_word(Line, 1),
        Day = list_to_integer(DayStr),
        MxT = to_temp(string:sub_word(Line, 2)),
        MnT = to_temp(string:sub_word(Line, 3)),
        {Day, MxT, MnT}
    end,

    Result = file_common:process(FileName, SortPred, IsDataLine, LineParser),
    io:format("~p~n", [element(1, Result)]).

to_temp(TempStr) ->
    %% 135* ==> 135
    %% 125  ==> 125
    list_to_integer(string:strip(TempStr, right, $*)).
